<template>
  <div class="pd-20">
    <Form :model="formItem"
          :label-width="200">
      <FormItem label="原密码">
        <Input v-model="formItem.oldPassword"
               placeholder="请输入原密码"
               type="password"></Input>
      </FormItem>
      <FormItem label="新密码">
        <Input v-model="formItem.newPassword"
               placeholder="请输入新密码"
               type="password"></Input>
      </FormItem>
      <FormItem label="再次输入新密码">
        <Input v-model="formItem.repeatNewPassword"
               placeholder="请重新输入新密码"
               type="password"></Input>
      </FormItem>
      <FormItem>
        <el-button type="primary"
                   @click="save">提 交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </FormItem>
    </Form>
  </div>
</template>

<script>
  import md5 from 'js-md5';

  export default {
    name: "modifyPassword",
    data () {
      return {
        formItem: {
          oldPassword: "",
          newPassword: "",
          repeatNewPassword: ""
        }
      }
    },
    created () {

    },
    methods: {
      save () {
        if (!this.formItem.oldPassword || this.formItem.oldPassword == '') {
          this.$Message.warning('原密码不能为空');
          return;
        }
        if (!this.formItem.newPassword || this.formItem.newPassword == '') {
          this.$Message.warning('新密码不能为空');
          return;
        }
        if (this.formItem.newPassword != this.formItem.repeatNewPassword) {
          this.$Message.warning('两次密码输入不一致');
          return;
        }
        if (this.formItem.oldPassword == this.formItem.newPassword) {
          this.$Message.warning('新密码不能原密码相同');
          return;
        }
        let param = {
          password: md5(this.formItem.oldPassword),
          newPassword: md5(this.formItem.newPassword),
        };

        this.$Modal.confirm({
          title: '确定',
          content: '<p>您确定修改密码？</p>',
          onOk: () => {
            this.ajax({
              type: this.api.modifyPassword.type,
              url: this.api.modifyPassword.url,
              data: param,
              success: (data) => {
                this.$Message.success('密码设置成功');
              },
              error: (data) => {
                this.$Message.error(data.message);
              }
            })
          },
          onCancel: () => {
          }
        });

      },
      cancel () {

      }
    }
  }
</script>

<style scoped>
</style>
